package com.chinda.common.enums;

import lombok.Getter;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.EnumSet;
import java.util.Locale;

/**
 * @author Wang Chinda
 * @date 2021/6/14
 * @see
 * @since 1.0
 */
@Getter
public enum MessageEnum {

    SYS_CHECK_0001(10001, "SYS.CHECK.0001"),
    SYS_CHECK_0002(10002, "SYS.CHECK.0002"),
    SYS_CHECK_0003(10003, "SYS.CHECK.0003"),
    SYS_CHECK_0004(10004, "SYS.CHECK.0004"),
    SYS_CHECK_0005(10005, "SYS.CHECK.0005"),
    SYS_CHECK_0006(10006, "SYS.CHECK.0006"),
    SYS_CHECK_0007(10007, "SYS.CHECK.0007"),
    SYS_CHECK_0008(10008, "SYS.CHECK.0008"),
    SYS_CHECK_0009(10009, "SYS.CHECK.0009"),
    SYS_CHECK_0010(10010, "SYS.CHECK.0010"),
    SYS_CHECK_0011(10011, "SYS.CHECK.0011"),
    SYS_CHECK_0012(10012, "SYS.CHECK.0012"),
    SYS_CHECK_0013(10013, "SYS.CHECK.0013"),
    SYS_CHECK_0014(10014, "SYS.CHECK.0014"),
    SYS_CHECK_0015(10015, "SYS.CHECK.0015"),
    SYS_CHECK_0016(10016, "SYS.CHECK.0016"),
    SYS_CHECK_0017(10017, "SYS.CHECK.0017"),
    SYS_CHECK_0018(10018, "SYS.CHECK.0018"),
    SYS_CHECK_0019(10019, "SYS.CHECK.0019"),
    SYS_CHECK_0022(10022, "SYS.CHECK.0022"),
    SYS_SECURITY_0001(20001, "SYS.SECURITY.0001"),
    SYS_SECURITY_0002(20002, "SYS.SECURITY.0002"),
    SYS_SECURITY_0003(20003, "SYS.SECURITY.0003"),
    SYS_SECURITY_0004(20004, "SYS.SECURITY.0004"),
    SYS_SECURITY_0005(20005, "SYS.SECURITY.0005"),
    SYS_SECURITY_0006(20006, "SYS.SECURITY.0006"),
    SYS_SECURITY_0007(20007, "SYS.SECURITY.0007"),
    SYS_SECURITY_0008(20008, "SYS.SECURITY.0008"),
    SYS_SECURITY_0009(20009, "SYS.SECURITY.0009"),
    SYS_SECURITY_0010(20010, "SYS.SECURITY.0010"),
    SYS_SECURITY_0011(20011, "SYS.SECURITY.0011"),
    SYS_SECURITY_0012(20012, "SYS.SECURITY.0012"),
    SYS_SECURITY_0013(20013, "SYS.SECURITY.0013"),
    SYS_SECURITY_0014(20014, "SYS.SECURITY.0014"),
    SYS_SECURITY_0015(20015, "SYS.SECURITY.0015");

    private int code;
    private String msg;

    private MessageEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getMsg() {
        return messageSource.getMessage(msg, null, Locale.getDefault());
    }

    public String getMsg(String... params) {
        return messageSource.getMessage(msg, params, Locale.getDefault());
    }

    private MessageSource messageSource;

    public MessageEnum setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
        return this;
    }

    @Component
    public static class EnumValuesInjectionService {

        private final MessageSource messageSource;

        public EnumValuesInjectionService(MessageSource messageSource) {
            this.messageSource = messageSource;
        }

        /** 通过静态内部类的方式注入到bean，并赋值到枚举中 */
        @PostConstruct
        public void postConstruct() {

            for (MessageEnum level : EnumSet.allOf(MessageEnum.class)) {
                level.setMessageSource(messageSource);
            }
        }
    }
}
